namespace Vanilla
/* TODO:
 *   - Make it load a file other than ./FileTypes.xml depending on registry settings
 */

import System.Collections.Generic
import System.Xml
import System

// OPEN ACTION
interface IOpen:
	def Open ():
		pass

// OPEN FILE ACTION
interface IOpenFile:
	def OpenFile (f as File):
		pass

// NO ACTION EXCEPTION
class NoActionException (Exception):
	def constructor (r as string):
		super(r)

class Action:
	public Tool as string
	
	def constructor (name as string, type as string, tool as string):
		Tool = tool

class FileType:
	public Name as string
	public Extensions as (string)
	_inherits as (string)
	
	public Actions = Dictionary[of string,Action]()
	
	def GetAction (action as string) as Action:
		return Actions[action] if Actions.ContainsKey(action)
		
		for i in Inherits:
			a = i.GetAction(action)
			return a unless a == null
		
		return null
	
	def GetActions (action as string) as (Action):
		a = array(Action, 0)
		
		a += (Actions[action],) if Actions.ContainsKey(action)
		
		for i in Inherits:
			a += i.GetActions(action)
		
		return a
	
	def HasToolForAction (action as string):
		return GetAction(action) != null
	
	def ToolForAction (action as string) as Tool:
		a = GetAction(action)
		
		raise NoActionException("No tool for \"${action}\" on type \"${Name}\"") if a == null
		
		return Tool.LoadTool(File(a.Tool))
	
	InheritedTypes as (FileType):
		get:
			inh = array(FileType,0)
			for i in Inherits:
				inh += (i,)
				inh += i.InheritedTypes
			return inh
	
	Inherits as (FileType):
		get:
			inh = array(FileType,0)
			for i in _inherits:
				if FileTypes.Named.ContainsKey(i):
					inh += (FileTypes.Named[i],) 
				else:
					inh += (FileType(i, array(string,0), array(string,0)),)
			return inh
	
	def IsAlso (t as string):
		return true if Name == t
		i = Inherits
		for j in i:
			return true if j.Name == t
		return false
	
	override def ToString ():
		return Name
	
	def constructor (n as string, e as (string), i as (string)):
		Name = n
		Extensions = e
		_inherits = i

class FileTypes:
	static public Types = List[of FileType]()
	
	static public Named = Dictionary[of string, FileType]()
	static public Ext = Dictionary[of string, FileType]()
	
	// Add a file type
	static def Add (f as FileType):
		print "Add - ${f.Name}/${f.Extensions}/${f.Inherits}"
		
		Types.Add(f)
		Named.Add(f.Name,f)
		for x in f.Extensions:
			unless x == "" or x == " ":
				print " -- adding extension ${x}"
				Ext.Add(x, f)
	
	// STATIC CONSTRUCTOR - loads the registry
	static def constructor ():
		print "--------Whoop---------"
	
		d = XmlDocument()
		d.Load("FileTypes.xml")
		
		e = d.DocumentElement
		print "e.Name = ${e.Name}"
		
		if e.Name != "filetypes":
			raise Exception("Head node of FileTypes.xml is ${e.Name} instead of filetypes")
		
		//i = e.FirstChild as XmlElement
		//while i != null:
		for i as XmlElement in e.GetElementsByTagName("type"):
			print "i.Name = ${i.Name}"
			name = i.GetAttribute("name")
			ext = i.GetAttribute("ext").Split(char(';'))
			inherit = i.GetAttribute("inherit").Split(char(';'))
			
			Add(FileType(name, ext, inherit))
			
			i = i.NextSibling as XmlElement
		
		for i as XmlElement in e.GetElementsByTagName("action"):
			names = i.GetAttribute("name").Split(char(';'))
			types = i.GetAttribute("type").Split(char(';'))
			tool = i.GetAttribute("tool")
			
			for j in names:
				for k in types:
					Named[k].Actions.Add(j, Action(j, k, tool))
